package jp.co.sharp.android.parents.kidsguard.activity.setting.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.activity.edit.ScheduleEditActivity;
import jp.co.sharp.android.parents.kidsguard.activity.setting.DeviceSelectorActivity;
import jp.co.sharp.android.parents.kidsguard.activity.setting.OkaeriSettingActivity;
import jp.co.sharp.android.parents.kidsguard.activity.setting.dialog.OkaeriNotificationDlg;
import jp.co.sharp.android.parents.kidsguard.common.DelDeviceConfirmDlg;
import jp.co.sharp.android.parents.kidsguard.dto.DeviceListDto;

public class OkaeriSettingItemAdaptor extends ArrayAdapter<DeviceListDto> {
    private int resourceId;
    private OnClickItemListenter onClickItemListenter;
    private Context mContext;


    public OkaeriSettingItemAdaptor(@NonNull Context context, int resource) {
        super(context, resource);
    }

    public OkaeriSettingItemAdaptor(@NonNull Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public OkaeriSettingItemAdaptor(@NonNull Context context, int resource, @NonNull DeviceListDto[] objects) {
        super(context, resource, objects);
    }

    public OkaeriSettingItemAdaptor(@NonNull Context context, int resource, int textViewResourceId, @NonNull DeviceListDto[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public OkaeriSettingItemAdaptor(@NonNull Context context, int resource, @NonNull List<DeviceListDto> objects, OnClickItemListenter onClickItemListenter) {
        super(context, resource, objects);
        this.resourceId = resource;
        this.onClickItemListenter = onClickItemListenter;
        this.mContext = context;
    }

    public OkaeriSettingItemAdaptor(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<DeviceListDto> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        DeviceListDto deviceListDto = getItem(position);

        View view;
        ViewHolder viewHolder;

        if (null == converView) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.deviceNickName = view.findViewById(R.id.item_name);
            viewHolder.deviceOrder = view.findViewById(R.id.deviceOrder);
            viewHolder.edit = view.findViewById(R.id.edit);
            viewHolder.del = view.findViewById(R.id.del);
            viewHolder.show = view.findViewById(R.id.show);
            viewHolder.itemLayout = view.findViewById(R.id.item_layout);
            view.setTag(viewHolder);
        } else {
            view = converView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.deviceNickName.setText(deviceListDto.getDeviceNickName());
        viewHolder.deviceOrder.setText("機器"+(position+1));
        viewHolder.edit.setOnClickListener(new EditClick(view));
        viewHolder.del.setOnClickListener(new Delete(view));
        viewHolder.itemLayout.setOnClickListener(new ItemLayoutClick());
        viewHolder.edit.setTag(position);
        viewHolder.del.setTag(position);
        viewHolder.itemLayout.setTag(position);

        if(deviceListDto.getDeviceNickName() != null && !deviceListDto.getDeviceNickName().equals("おとうさん")){
            viewHolder.show.setVisibility(View.VISIBLE);
        }else {
            viewHolder.show.setVisibility(View.INVISIBLE);
        }
        return view;
    }

    class ViewHolder {
        LinearLayout itemLayout;
        LinearLayout show;
        TextView deviceNickName;
        TextView deviceOrder;
        ImageView edit;
        ImageView del;

    }

    public interface OnClickItemListenter {
        void chooseItemClick(int position, View view);
    }

    class EditClick implements View.OnClickListener {
        View view ;
        EditClick(View v){
            view=v;
        }
        @Override
        public void onClick(View v) {
            DeviceListDto dto = getItem((Integer) v.getTag());
            OkaeriNotificationDlg okaeriNotificationDlg = new OkaeriNotificationDlg(mContext,"ID:"+dto.getDeviceId(),dto.getDeviceNickName());
            okaeriNotificationDlg.show();
            okaeriNotificationDlg.setOkaeriNameSetListener(new OkaeriNotificationDlg.OkaeriNameSetListener() {
                @Override
                public void OkaeriNameSet(String name) {
                    TextView textView = view.findViewById(R.id.item_name);
                    textView.setText(name);
                    dto.setDeviceNickName(name);
                }
            });

        }
    }

    class Delete implements View.OnClickListener{
        View view ;
        Delete(View v){
            view=v;
        }
        @Override
        public void onClick(View v) {
            String message = "機器を削除します。よろしいですか？";
            DelDeviceConfirmDlg  mDelDeviceConfirmDlg = new DelDeviceConfirmDlg(mContext, R.style.commonDlg, message);
            mDelDeviceConfirmDlg.show();
            mDelDeviceConfirmDlg.setOnOperateSetListener(new DelDeviceConfirmDlg.OnOperateSetListener() {
                @Override
                public void onOperateSet() {
                    DeviceListDto dto = getItem((Integer) v.getTag());
                    dto.setDeviceNickName("おとうさん");

                    LinearLayout linearLayout = view.findViewById(R.id.show);
                    linearLayout.setVisibility(View.INVISIBLE);
                    TextView textView = view.findViewById(R.id.item_name);
                    textView.setText("おとうさん");
                    mDelDeviceConfirmDlg.dismiss();
                }
            });
        }
    }

    class ItemLayoutClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, DeviceSelectorActivity.class);
            intent.putExtra("position",(Integer)v.getTag());
            int DeviceSelectorRequestCode=1;
            OkaeriSettingActivity.mPageJumpFlag = true;
            ((Activity) mContext).startActivityForResult(intent, DeviceSelectorRequestCode);
        }
    }
}















































